package coms.delegates;

import EJB.EJBLocator;
import coms.dto.Conference;
import coms.dto.Email;
import coms.dto.Keyword;
import coms.dto.User;
import coms.ejb.UserBeanRemote;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.*;

public class UserDelegate implements IDelegate<User>{

    private UserBeanRemote userBean;
    private Queue queue;
    private QueueConnectionFactory factory;
    private QueueConnection connection;
    private QueueSession session;
    private QueueSender sender;
    private String emailAddress;
    private MapMessage msg;
    
    public UserDelegate() {
        try {
            this.userBean = EJBLocator.lookupUserBean();
            this.queue = (Queue)EJBLocator.lookupEmailMDB();
            this.factory = (QueueConnectionFactory)EJBLocator.lookupEmailFactory();
            this.connection = (QueueConnection)factory.createQueueConnection();
            this.session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
            this.sender = session.createSender(queue);  
        } catch (Exception ex) {
            Logger.getLogger(UserDelegate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean create(User user) {
        userBean.create(user);
        return true;
    }

    public boolean delete(User user) {
        boolean deleted = userBean.delete(user);
        return deleted;
    }

    public boolean update(User user) {
        userBean.update(user);
        return true;
    }

    @Override
    public List<User> search(User user) throws Exception {
        return userBean.search(user);
    }

    @Override
    public List<User> findAll() throws Exception {
        return userBean.findAll();
    }

    @Override
    public User getById(int id) throws Exception {
       return userBean.getById(id);
    }
    
    public User getByUsername(String username)throws Exception{
        return userBean.findUserByUsername(username);
    }
    
    public User validateUser(User user)
    {   User usr=null;
        try{
            usr =  userBean.validateUser(user.getUsername(), user.getPassword());
        }catch(Exception e)
        {
            e.printStackTrace();
            
        }
         return usr;   
    }

    public void sendEmail(Email email) throws JMSException {
            
            if(email==null){
                System.out.println("No Email...");
                return;
            }
             //create and set a message to send
            msg = session.createMapMessage();
            emailAddress = email.getTo();
            
            
            msg.setString("recipient", emailAddress);
            System.out.println(email.getMessage());
            msg.setString("message", email.getMessage());
            msg.setString("subject", email.getSubject());
            //finally client send messages asynchronously to the queue
            sender.send(msg);
            
            System.out.println("*******************Email Created");

    }
}
